﻿Imports System.Windows.Forms
Imports System.Collections.Specialized
Imports System.ComponentModel

Public Class InputUpdate
    Private Sub InputUpdate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim 开始时间 As Date = New Date(Now.Year, Now.Month, 1)
        'Dim 结束时间 As Date = New Date(Now.Year, Now.Month, Date.DaysInMonth(Now.Year, Now.Month))

        'Me.dt开始时间.Value = 开始时间
        'Me.dt结束时间.Value = 结束时间
        Me.txtUsername.Tag = ""
        rad收入时间年_Click(Nothing, Nothing)
        LoadData1(1)
        OutUpdat_Click(Nothing, Nothing)
        If Me.rad全部折叠.Checked Then
            Me.OutlookGrid1.CollapseAll()
        Else
            Me.OutlookGrid1.ExpandAll()
        End If
    End Sub

#Region "查询上一个月按钮"
    Public Sub LoadData1(ByVal itype As Integer)

        Dim strBegin As String
        Dim strEnd As String
        If rad收入时间日.Checked Then
            strBegin = Me.dt开始时间.Value.Year.ToString("0000") + "-" + Me.dt开始时间.Value.Month.ToString("00") & "-" & Me.dt开始时间.Value.Day.ToString("00")
            strEnd = Me.dt结束时间.Value.Year.ToString("0000") + "-" + Me.dt结束时间.Value.Month.ToString("00") & "-" & Me.dt结束时间.Value.Day.ToString("00")
        ElseIf rad收入时间月.Checked Then
            strBegin = Me.dt开始时间.Value.Year.ToString("0000") + "-" + Me.dt开始时间.Value.Month.ToString("00") & "-" & "00"
            strEnd = Me.dt结束时间.Value.Year.ToString("0000") + "-" + Me.dt结束时间.Value.Month.ToString("00") & "-" & "31"
        Else
            strBegin = Me.dt开始时间.Value.Year.ToString("0000") & "-" & "00" & "-" & Me.dt开始时间.Value.Day.ToString("00")
            strEnd = Me.dt结束时间.Value.Year.ToString("0000") & "-" & "12" & "-" & "31"
        End If

        Dim sb As New System.Text.StringBuilder
        sb.AppendLine(" SELECT WysInput.*  ")
        sb.AppendLine(" ,XscBugType.Name ")
        sb.AppendLine(" ,WysInputUser.username username1")
        sb.AppendLine(" FROM WysInput ")
        sb.AppendLine(" LEFT JOIN XscBugType ")
        sb.AppendLine(" ON WysInput.type=XscBugType.id ")
        sb.AppendLine(" LEFT JOIN WysInputUser ")
        sb.AppendLine(" ON WysInput.username=WysInputUser.id ")
        sb.AppendLine(" WHERE date([DateTime])>='" + strBegin + "'")
        sb.AppendLine(" AND date([DateTime])<='" + strEnd + "'")
        If chk用户.Checked AndAlso Me.txtUsername.Tag.ToString.Length = 36 Then
            sb.AppendLine(" AND WysInputUser.id='" & Me.txtUsername.Tag.ToString & "'")
        End If
        Dim dtDataTable As DataTable = ct.FillTable(sb.ToString)
        Dim dv As DataView = dtDataTable.DefaultView
        dtDataTable = dv.ToTable

        Dim dtNew As New DataTable
        dtNew.TableName = "收入"
        dtNew.Columns.Add("编号")
        dtNew.Columns.Add("收入时间日")
        dtNew.Columns.Add("收入时间月")
        dtNew.Columns.Add("收入时间年")
        dtNew.Columns.Add("收入去向")
        dtNew.Columns.Add("收入类别")
        dtNew.Columns.Add("金额")
        dtNew.Columns.Add("备注")
        dtNew.Columns.Add("收入用户")
        For Each dr As DataRow In dtDataTable.Rows
            dtNew.Rows.Add(New Object() {转换对象字符串(dr("ID")), 转换日期字符串(dr("DATETIME")), 转换日期字符串1(dr("DATETIME")), 转换日期字符串2(dr("DATETIME")), 转换对象字符串(dr("TITLE")), 转换对象字符串(dr("NAME")), _
                                                                    转换对象字符串(dr("COUNT")), 转换对象字符串(dr("MEMO")), 转换对象字符串(dr("USERNAME1"))})
        Next
        Me.OutlookGrid1.AutoGenerateColumns = False
        Me.OutlookGrid1.BindData(dtNew, "收入")
        Me.OutlookGrid1.Columns(0).Visible = False
    End Sub

    Private Function 转换对象字符串(ByVal ob As Object) As String
        If ob Is DBNull.Value Then
            Return ""
        End If
        Return ob.ToString
    End Function

    Private Function 转换日期字符串(ByVal ob As Object) As String
        Dim dt As Date = Date.Parse(ob.ToString())
        Return dt.ToString("yyyy年MM月dd日")
    End Function

    Private Function 转换日期字符串1(ByVal ob As Object) As String
        Dim dt As Date = Date.Parse(ob.ToString())
        Return dt.ToString("yyyy年MM月")
    End Function

    Private Function 转换日期字符串2(ByVal ob As Object) As String
        Dim dt As Date = Date.Parse(ob.ToString())
        Return dt.ToString("yyyy年")
    End Function
#End Region

#Region "删除CHECK"
    Public Overrides Function DoDeleteCheck() As Boolean
        DoDeleteCheck = False
        If Me.OutlookGrid1.SelectedRows.Count = 0 Then
            ShowMessageBox("没有选中任何记录")
            Exit Function
        End If

        If Me.OutlookGrid1.SelectedRows.Count = 0 Then Exit Function
        Dim id As String = OutlookGrid1.SelectedRows(0).Cells(0).Value.ToString
        If id.Length <> 36 Then
            ShowMessageBox("没有选中任何记录")
            Exit Function
        End If
        strid = id
        If ShowMessageResult("是否删除本条记录") = Windows.Forms.DialogResult.No Then Exit Function

        DoDeleteCheck = True
    End Function
#End Region

#Region "重载删除事件"

    Private Sub UpdateCrashInOut()

        Dim sb As New StringBuilder
        Dim dt As New DataTable
        Dim rs As ADODB.Recordset = ct.GetTable("SELECT * from CrashInOut  where InOutID='" + strid + "'")
        dt = ct.FillTable(rs)
        If dt.Rows.Count > 0 Then
            Dim cmdSub As ADODB.Command = New ADODB.Command
            cmdSub.CommandType = ADODB.CommandTypeEnum.adCmdText
            For Each dr As DataRow In dt.Rows
                If CStr(dr("CrashStatusID")) <> "" AndAlso CDec(dr("CrashStatusCount")) > 0 Then
                    sb.Length = 0
                    sb.AppendLine("Update CrashStatus set Count=Count -" & CStr(dr("CrashStatusCount")) & " Where id='" & CStr(dr("CrashStatusID")) + "'")
                    cmdSub.CommandText = sb.ToString
                    ct.ExcCommand(cmdSub)
                    UpdateCrashOptime(ct, CStr(dr("CrashStatusID")))
                    '2011-11-20
                    Account_Detail.Update收入_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), dr("USERNAME").ToString, CDec(dr("CrashStatusCount")))
                    Crash_Detail.Update收入_减(ct, CDate(dr("DATETIME")).ToString("yyyy-MM-dd"), dr("CrashStatusID").ToString, CDec(dr("CrashStatusCount")))
                End If
            Next
        End If
        ct.GetTable("DELETE from CrashInOut  where InOutID='" + strid + "'")
    End Sub

    Dim strid As String = ""
    Public Overrides Function DoDelete() As Boolean

        UpdateCrashInOut()

        Dim CMD As New ADODB.Command
        CMD.CommandText = "DELETE from WysInput  where ID='" + strid + "'"
        If ct.ExcCommand(CMD) Then
            ShowMessageBox("删除成功")
        Else
            ShowMessageBox("删除失败")
        End If

        LoadData1(0)
        OutUpdat_Click(Nothing, Nothing)
        Return True
    End Function
#End Region

#Region "重载插入"
    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        Using frm As New InputAdd
            frm.iType = Me.iType
            frm.strName = Me.strName
            frm.ShowDialog()
            If frm.bAdd Then
                LoadData1(0)
                OutUpdat_Click(Nothing, Nothing)
            End If
        End Using
        Return True
    End Function
#End Region

    Private Sub MyDataView1_ItemDoubleClickRefreshData(ByVal strId As System.String, ByRef bState As System.Boolean) Handles MyDataView1.ItemDoubleClickRefreshData

    End Sub

#Region "重载设置窗体函数 A-2010-09-02"
    Public Overrides Function 初始行数() As Integer
        Return MyDataView1.PageSize
    End Function
    Public Overrides Sub 设置窗体数据行数()
        MyBase.设置窗体数据行数()
        MyDataView1.PageSize = Get窗体行数(MyDataView1.PageSize)
    End Sub

    Public Overrides Function 是否显示设置窗体数据行数() As Boolean
        Return False
    End Function
#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        LoadData1(0)
        OutUpdat_Click(Nothing, Nothing)
    End Sub


    Private Sub dt开始时间_ValueChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dt结束时间.ValueChanged, dt开始时间.ValueChanged
        Dim 开始时间 As Integer = CInt(dt开始时间.Value.ToString("yyyyMMdd"))
        Dim 结束时间 As Integer = CInt(dt结束时间.Value.ToString("yyyyMMdd"))
        If 开始时间 > 结束时间 Then
            Exit Sub
        End If
    End Sub

    Private Sub OutUpdat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If Me.OutlookGrid1.RowCount = 0 Then Exit Sub
        Dim al As New List(Of DataGridViewColumn)
        Dim al1 As New List(Of ListSortDirection)

        If Me.chk收入用户.Checked Then
            al.Add(Me.OutlookGrid1.Columns("收入用户"))
            al1.Add(ListSortDirection.Ascending)
        End If

        If Me.chk收入类别.Checked Then
            al.Add(Me.OutlookGrid1.Columns("收入类别"))
            al1.Add(ListSortDirection.Descending)
        End If

        If rad收入时间年.Checked Then
            al.Add(Me.OutlookGrid1.Columns("收入时间年"))
            al1.Add(ListSortDirection.Descending)
        ElseIf rad收入时间月.Checked Then
            al.Add(Me.OutlookGrid1.Columns("收入时间月"))
            al1.Add(ListSortDirection.Descending)
        Else
            al.Add(Me.OutlookGrid1.Columns("收入时间日"))
            al1.Add(ListSortDirection.Descending)
        End If

        OutlookGrid1.GroupTemplate.Column = al.ToArray
        OutlookGrid1.Sort(al.ToArray, al1.ToArray)
        'If Me.rad全部折叠.Checked Then
        '    Me.OutlookGrid1.CollapseAll()
        'Else
        '    Me.OutlookGrid1.ExpandAll()
        'End If
    End Sub

    Private Sub rad全部折叠_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rad全部折叠.Click, rad全部展开.Click
        If Me.rad全部折叠.Checked Then
            Me.OutlookGrid1.CollapseAll()
        Else
            Me.OutlookGrid1.ExpandAll()
        End If
    End Sub

    Private Sub OutlookGrid1_SGT(ByRef content As System.String, ByVal olg As Wys.Control.CSharp.OutlookGridRow, ByVal RowIndex As System.Collections.Generic.List(Of Integer)) Handles OutlookGrid1.SGT
        If Not olg.IsLastRow Then
            Dim 金额 As Integer = 0
            For Each ri As Integer In RowIndex
                金额 += CDec(Me.OutlookGrid1.Rows(ri).Cells("金额").Value)
            Next
            content = olg.Group.Value.ToString.Trim & New String(" ", (60 - 得到GB2312编码.GetByteCount(olg.Group.Value.ToString.Trim))) & "  合计收入：" & 金额.ToString
        Else
            Dim 总金额 As Decimal = 0
            For Each dr As DataGridViewRow In Me.OutlookGrid1.Rows
                总金额 += CDec(dr.Cells("金额").Value)
            Next
            content = New String(" ", 60) & " 总合计收入：" & 总金额.ToString
        End If
    End Sub

    Private Sub OutlookGrid1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OutlookGrid1.DoubleClick
        If Me.OutlookGrid1.SelectedRows.Count = 0 Then Exit Sub
        Dim id As String = OutlookGrid1.SelectedRows(0).Cells(0).Value.ToString
        If id.Length = 36 Then
            Using frm As InputAdd = New InputAdd
                frm.参数列表.Add("ID", id)
                frm.模式 = Wys.Control.Vb.InsertFormBase.模式型.修正
                frm.iType = Me.iType
                frm.strName = Me.strName
                frm.ShowDialog()
                If frm.bAdd Then
                    LoadData1(0)
                    OutUpdat_Click(Nothing, Nothing)
                End If
            End Using
        End If
    End Sub

    Private Sub chk用户_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk用户.CheckedChanged
        txtUsername.Enabled = chk用户.Checked
        btnUsername.Enabled = chk用户.Checked
        chk收入用户.Visible = Not chk用户.Checked
        If Not chk用户.Checked Then
            txtUsername.Text = ""
            txtUsername.Tag = ""
        End If
    End Sub

    Private Sub btnUsername_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUsername.Click
        Using frm As Wys.Control.Vb.ListForm = New Wys.Control.Vb.ListForm
            frm.Text = "家庭成员一览"
            frm.strName = Me.strName
            frm.iType = Me.iType
            'frm.SQL = "select ID,UserName as 名称,Count as 金额 from WysInputUser"
            frm.SQL = "select ID,username as 名称,memo as 备注 from WysInputUser  where type=0 order by 名称"
            Dim 显示列 As New StringCollection
            显示列.Add("名称")
            显示列.Add("备注")
            frm.Width = 250
            frm.Height = 200
            frm.显示列 = 显示列
            frm.ShowDialog()
            If frm.返回值.Count > 0 Then
                txtUsername.Tag = CStr(frm.返回值(0))
                txtUsername.Text = CStr(frm.返回值(1))
            End If
        End Using
    End Sub

    Private Sub rad收入时间年_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rad收入时间月.Click, rad收入时间日.Click, rad收入时间年.Click
        Dim 开始时间 As Date = New Date(Now.Year, Now.Month, 1)
        Dim 结束时间 As Date = New Date(Now.Year, Now.Month, Date.DaysInMonth(Now.Year, Now.Month))

        If rad收入时间日.Checked Then
            Me.dt开始时间.Format = DateTimePickerFormat.Custom
            Me.dt开始时间.CustomFormat = "yyyy年MM月dd日"

            Me.dt结束时间.Format = DateTimePickerFormat.Custom
            Me.dt结束时间.CustomFormat = "yyyy年MM月dd日"

            开始时间 = New Date(Now.Year, Now.Month, 1)
            结束时间 = New Date(Now.Year, Now.Month, Date.DaysInMonth(Now.Year, Now.Month))

        ElseIf rad收入时间月.Checked Then
            Me.dt结束时间.Format = DateTimePickerFormat.Custom
            Me.dt结束时间.CustomFormat = "yyyy年MM月"

            Me.dt开始时间.Format = DateTimePickerFormat.Custom
            Me.dt开始时间.CustomFormat = "yyyy年MM月"

            开始时间 = New Date(Now.Year, 1, 1)
            结束时间 = New Date(Now.Year, 12, Date.DaysInMonth(Now.Year, Now.Month))

        Else
            Me.dt结束时间.Format = DateTimePickerFormat.Custom
            Me.dt结束时间.CustomFormat = "yyyy年"

            Me.dt开始时间.Format = DateTimePickerFormat.Custom
            Me.dt开始时间.CustomFormat = "yyyy年"

            开始时间 = New Date(Now.Year, 1, 1)
            结束时间 = New Date(Now.Year, 12, Date.DaysInMonth(Now.Year, Now.Month))
        End If
        'Me.dt开始时间.Value = 开始时间
        'Me.dt结束时间.Value = 结束时间

    End Sub

    Public Overrides Sub 保存窗体数据到数据库(ByRef dtData As System.Data.DataTable)
        '年
        dtData.Rows.Add(New String() {"开始时间", "", dt开始时间.Value.ToString("yyyy-MM-dd")})
        dtData.Rows.Add(New String() {"结束时间", "", dt结束时间.Value.ToString("yyyy-MM-dd")})
        If chk收入类别.Checked Then
            dtData.Rows.Add(New String() {"收入类别", "", "是"})
        Else
            dtData.Rows.Add(New String() {"收入类别", "", "否"})
        End If

        If chk收入用户.Checked Then
            dtData.Rows.Add(New String() {"收入用户", "", "是"})
        Else
            dtData.Rows.Add(New String() {"收入用户", "", "否"})
        End If

        If Me.rad收入时间月.Checked Then
            dtData.Rows.Add(New String() {"收入时间月", "", "1"})
        ElseIf Me.rad收入时间年.Checked Then
            dtData.Rows.Add(New String() {"收入时间月", "", "2"})
        Else
            dtData.Rows.Add(New String() {"收入时间月", "", "3"})
        End If

        If Me.rad全部展开.Checked Then
            dtData.Rows.Add(New String() {"全部展开", "", "是"})
        Else
            dtData.Rows.Add(New String() {"全部展开", "", "否"})
        End If
        MyBase.保存窗体数据到数据库(dtData)
    End Sub

    Public Overrides Sub 存取数据库数据到窗体(ByVal dtDbData As System.Data.DataTable)

        If dtDbData.Rows.Count > 0 Then
            '年
            dt开始时间.Value = 查找数据("开始时间", "", Now.ToString("yyyy-MM-dd"), dtDbData)
            dt结束时间.Value = 查找数据("结束时间", "", Now.ToString("yyyy-MM-dd"), dtDbData)
            chk收入类别.Checked = IIf(查找数据("收入类别", "", "否", dtDbData) = "否", False, True)
            chk收入用户.Checked = IIf(查找数据("收入用户", "", "否", dtDbData) = "否", False, True)

            rad收入时间月.Checked = IIf(查找数据("收入时间月", "", "1", dtDbData) = "1", True, False)
            rad收入时间年.Checked = IIf(查找数据("收入时间月", "", "1", dtDbData) = "2", True, False)
            rad收入时间日.Checked = IIf(查找数据("收入时间月", "", "1", dtDbData) = "3", True, False)

            rad全部展开.Checked = IIf(查找数据("全部展开", "", "是", dtDbData) = "是", True, False)
            Me.rad全部折叠.Checked = IIf(查找数据("全部展开", "", "否", dtDbData) = "否", True, False)
        End If
        MyBase.存取数据库数据到窗体(dtDbData)
    End Sub
End Class
